package com.qf.mvc2203.utils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;

public class MySerializer extends JsonSerializer<Double> {
    @Override
    public void serialize(Double value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {

        // Double四舍五入
        final String s = BigDecimal.valueOf(value).setScale(2, BigDecimal.ROUND_HALF_UP).toString();

        // 如果是数值类型，需要使用writeNumber进行返回
        jsonGenerator.writeNumber(s);

    }
}
